<template>
	<view class="container box">
		<view class="date">
			<uni-datetime-picker :clear-icon="false" @change="changeFn" v-model="date"
				type="date"></uni-datetime-picker>
		</view>
		<template v-for="item  in healthData">
			<view class="section">
				{{item.label}}<label style="color:#A9A9A9">({{item.unit}})</label>
				<view class="info-value" @click="handleDialog(item.label,item.unit)">
					{{item.unit=='cm'?heightV:weighV}}
				</view>
			</view>
		</template>

		<healthInput @delay='delayDialog' :inputLabel='inputLabel' :inputUnit='inputUnit' class="diglog-input"
			v-if="showDigInput">
		</healthInput>
	</view>
</template>
<script>
	import {
		acquireData,
		addData,
		check,
		verify,
		tranformDateTime,
		compareDate,
		getHealthDatas
	} from '../../../database/index.js'
	import healthInput from '@/components/healthInput.vue'
	export default {
		components: {
			healthInput
		},
		data() {
			return {
				date: '',
				index: 0,
				heightV: '',
				weighV: '',
				currenetDate: '',
				inputUnit: '',
				inputLabel: '',
				showDigInput: false,
				healthData: [{
						label: '身高',
						unit: 'cm'
					},
					{
						label: '体重',
						unit: 'kg'
					}
				]
			}
		},
		async onPullDownRefresh() {
			this.date = ''
			this.index = 0
			this.heightV = ''
			this.weighV = ''
			this.currenetDate = ''
			this.inputUnit = ''
			this.inputLabel = ''
			this.showDigInput = false
			this.healthData = [{
					label: '身高',
					unit: 'cm'
				},
				{
					label: '体重',
					unit: 'kg'
				}
			]
			this.currenetDate = new Date().toLocaleDateString().replace(/\//g, '-')
			let datas = await this.getHealthData(this.currenetDate)
			this.heightV = datas[1]
			this.weighV = datas[0]
			this.date = this.currenetDate
			uni.stopPullDownRefresh()
		},
		async onLoad() {
			this.currenetDate = new Date().toLocaleDateString().replace(/\//g, '-')
			let datas = await this.getHealthData(this.currenetDate)
			this.heightV = datas[1]
			this.weighV = datas[0]
			this.date = this.currenetDate
		},
		methods: {
			async changeFn() {
				let today = new Date().toLocaleDateString().split('T')[0].replace(/\//g, '-')
				if (compareDate(tranformDateTime(new Date(this.date).toLocaleDateString()), today) == 1) {
					this.$showMsg('不能记录超过今天的日期')
					this.$forceUpdate()
					return
				} else {
					let datas = await this.getHealthData(this.date)
					this.heightV = datas[1]
					this.weighV = datas[0]
					this.currenetDate = this.date
					this.$forceUpdate()
				}

			},
			async getHealthData(date) {
				let minday = new Date(date)
				minday.setDate(new Date(date).getDate() - 1)
				let plusday = new Date(date)
				plusday.setDate(new Date(date).getDate() + 1)
				let datas = await getHealthDatas(0, date);
				let day = new Date(date).getDay()
				if (day == 0) {
					day = 7
				}
				return [datas[1][day - 1], datas[2][day - 1]]

			},
			handleDialog(label, unit) {
				this.inputLabel = label
				this.inputUnit = unit
				this.showDigInput = true
			},

			delayDialog(...data) {
				if (data.length > 2 && data[1] == '身高') {
					this.heightV = data[2]
					this.verifyHeight()
				}
				if (data.length > 2 && data[1] == '体重') {
					this.weighV = data[2]
					this.verifyWeigh()
				}
				this.showDigInput = false
			},
			async verifyHeight() {
				let today = new Date().toLocaleDateString().split('T')[0].replace(/\//g, '-')
				if (compareDate(tranformDateTime(new Date(this.currenetDate).toLocaleDateString()), today) == 1) {
					this.$showMsg('不能记录超过今天的日期')
					return
				} else {
					this.currenetDate = this.date
					let heightRs = await acquireData('heightRecord', {
						user_Id: this.$store.getters.getToken
					})
					if (heightRs.length > 0) {
						let isverify = false
						for (let rs of heightRs) {
							if (compareDate(tranformDateTime(new Date(rs.uploadTime).toLocaleDateString()),
									tranformDateTime(new Date(this.currenetDate).toLocaleDateString())) == 0) {
								isverify = true
								let [isHupdate, msg] = await verify('heightRecord', {
									_id: rs._id
								}, {
									height_value: this.heightV
								})
								if (isHupdate) {
									this.$showMsg('修改身高成功')
								} else {
									this.$showMsg('修改身高失败')
								}
							}
						}
						if (!isverify) {
							let [isHadd, addMsg] = await addData('heightRecord', {
								height_value: this.heightV,
								user_Id: this.$store.getters.getToken,
								uploadTime: new Date(this.currenetDate)
							})
							if (isHadd) {
								this.$showMsg('添加身高成功')
							} else {
								this.$showMsg('添加身高失败')
							}
						}
					} else {
						let [isHadd, addMsg] = await addData('heightRecord', {
							height_value: this.heightV,
							user_Id: this.$store.getters.getToken,
							uploadTime: new Date(this.currenetDate)
						})
						if (isHadd) {
							this.$showMsg('添加身高成功')
						} else {
							this.$showMsg('添加身高失败')
						}
					}
				}
				if (compareDate(tranformDateTime(new Date(this.currenetDate).toLocaleDateString()), today) ==
					0) {
					await verify('user', {
						_id: this.$store.getters.getToken
					}, {
						height: this.heightV
					})
				}
			},
			async verifyWeigh() {
				let today = new Date().toLocaleDateString().split('T')[0].replace(/\//g, '-')
				if (compareDate(tranformDateTime(new Date(this.currenetDate).toLocaleDateString()), today) == 1) {
					this.$showMsg('不能记录超过今天的日期')
					return
				} else {
					this.currenetDate = this.date
					let weighRs = await acquireData('weighRecord', {
						user_Id: this.$store.getters.getToken
					})
					if (weighRs.length > 0) {
						let isverify = false
						for (let rs of weighRs) {
							if (compareDate(tranformDateTime(new Date(rs.uploadTime).toLocaleDateString()),
									tranformDateTime(new Date(this.currenetDate).toLocaleDateString())) == 0) {
								isverify = true
								let [isWupdate, msg1] = await verify('weighRecord', {
									user_Id: this.$store.getters.getToken,
									_id: rs._id
								}, {
									weigh_value: this.weighV
								})
								if (isWupdate) {
									this.$showMsg('修改体重成功')
								} else {
									this.$showMsg('修改体重失败')
								}
							}
						}
						if (!isverify) {
							let [isWadd, addMsg] = await addData('weighRecord', {
								weigh_value: this.weighV,
								user_Id: this.$store.getters.getToken,
								uploadTime: new Date(this.currenetDate)
							})
							if (isWadd) {
								this.$showMsg('添加体重成功')
							} else {
								this.$showMsg('添加体重失败')
							}
						}
					} else {
						let [isWadd, addMsg] = await addData('weighRecord', {
							weigh_value: this.weighV,
							user_Id: this.$store.getters.getToken,
							uploadTime: new Date(this.currenetDate)
						})
						if (isWadd) {
							this.$showMsg('添加体重成功')
						} else {
							this.$showMsg('添加体重失败')
						}
					}
				}

				if (compareDate(tranformDateTime(new Date(this.currenetDate).toLocaleDateString()), today) ==
					0) {
					await verify('user', {
						_id: this.$store.getters.getToken
					}, {
						weigh: this.weighV
					})
				}
			},

		}
	}
</script>

<style lang="scss" scoped>
	.box {
		padding: 0;
	}

	.section {
		position: relative;
		color: #292929;
		padding: 20rpx 30rpx;
		padding-right: 30rpx;
		border-bottom: 2rpx solid #e7e7e7;
		background-color: white;

		.info-value {
			position: absolute;
			right: 60rpx;
			top: 20rpx;
		}

		&:last-child {
			border-bottom: none;
		}

		&::after {
			content: '>';
			font-weight: 600;
			color: #d8d8d8;
			position: absolute;
			right: 20rpx;
			top: 20rpx;
		}
	}



	.diglog-input {
		position: absolute;
		top: 20%;
		left: 25%;
	}
</style>